// 导入pixi.js
import * as PIXI from "pixi.js";
// import {PixiApp} from "@/pixi/App";
import {storeToRefs} from "pinia";
import  {usePixiStore}  from '@/store/pixi'
// 虚线容器
import {pathContainer} from '@/pixi/pathContainer'
import { toRaw } from 'vue'
import * as PIXILEGACY from "pixi.js-legacy";

export class Path{
    constructor(params) {
        this.tilingSprite = null;
        this.stopAnimationFrameLine = null;
        this.initPath(params)
    }

    static createPath(params){
        return new Path(params)
    }

    initPath({x0, y0, x1, y1, linewidth,bgUrl,speed=1}) {
        let store = usePixiStore()
        store.changeCurrentWidth()
        store.changeCurrentHeight()
        let {pixiMaxWidth,pixiMaxHeight,currentWidth,currentHeight } = storeToRefs(store)

        let XISHU = currentWidth.value/pixiMaxWidth.value;
        // toRaw(bgUrl).baseTexture.wrapMode = PIXI.WRAP_MODES.CLAMP;
        // toRaw(bgUrl).baseTexture.wrapMode = PIXI.WRAP_MODES.MIRRORED_REPEAT;
        // toRaw(bgUrl).baseTexture.wrapMode = PIXI.WRAP_MODES.REPEAT;

        let linelength = Math.pow(Math.pow(x1/pixiMaxWidth.value*currentWidth.value - x0/pixiMaxWidth.value*currentWidth.value, 2) + Math.pow(y1/pixiMaxHeight.value*currentHeight.value - y0/pixiMaxHeight.value*currentHeight.value, 2), 0.5);
        // this.tilingSprite = new PIXI.TilingSprite(toRaw(bgUrl), linelength, linewidth/pixiMaxHeight.value*currentHeight.value);
        this.tilingSprite = new PIXI.TilingSprite(toRaw(bgUrl), linelength/XISHU, linewidth);

        this.tilingSprite.scale.x = XISHU
        this.tilingSprite.scale.y = XISHU

        this.tilingSprite.x = x0/pixiMaxWidth.value*currentWidth.value - currentWidth.value/2 + (linewidth/pixiMaxWidth.value*currentWidth.value)/2;
        this.tilingSprite.y = y0/pixiMaxHeight.value*currentHeight.value - currentHeight.value/2 + (linewidth/pixiMaxWidth.value*currentWidth.value)/2 ;
        this.tilingSprite.rotation = this.angle(x0/pixiMaxWidth.value*currentWidth.value, y0/pixiMaxHeight.value*currentHeight.value, x1/pixiMaxWidth.value*currentWidth.value, y1/pixiMaxHeight.value*currentHeight.value) * Math.PI / 180;
        this.tilingSprite.pivot.set(0, linewidth/pixiMaxWidth.value*currentWidth.value / 2 / XISHU);



        // this.tilingSprite.x = x0 - currentWidth.value/2 + linewidth/2;
        // this.tilingSprite.y = y0 - currentHeight.value/2 + linewidth/2;
        // this.tilingSprite.rotation = this.angle(x0, y0, x1, y1) * Math.PI / 180;
        // this.tilingSprite.pivot.set(0, linewidth / 2);

        // 这种情况下可以设置 speed为1
        this.tilingSprite.speed = speed;

        // speed = speed?speed:linelength;
        // this.tilingSprite.speed = linelength/speed;

        pathContainer.addChild(this.tilingSprite)

        // 默认是开启 循环的
        this.loop()
        return this.tilingSprite;
    }

    angle(x0, y0, x1, y1) {
        let diff_x = Math.abs(x1 - x0),
            diff_y = Math.abs(y1 - y0);
        let cita;
        if (x1 > x0) {
            if (y1 > y0) {
                cita = 360 * Math.atan(diff_y / diff_x) / (2 * Math.PI);
            } else {
                if (y1 < y0) {
                    cita = -360 * Math.atan(diff_y / diff_x) / (2 * Math.PI);
                } else {
                    cita = 0;
                }
            }
        } else {
            if (x1 < x0) {
                if (y1 > y0) {
                    cita = 180 - 360 * Math.atan(diff_y / diff_x) / (2 * Math.PI);
                } else {
                    if (y1 < y0) {
                        cita = 180 + 360 * Math.atan(diff_y / diff_x) / (2 * Math.PI);
                    } else {
                        cita = 180;
                    }
                }
            } else {
                if (y1 > y0) {
                    cita = 90;
                } else {
                    if (y1 < y0) {
                        cita = -90;
                    } else {
                        cita = 0;
                    }
                }
            }
        }
        return cita;
    }

    loop(){
        this.tilingSprite.tilePosition.x  += this.tilingSprite.speed;
        this.stopAnimationFrameLine = window.requestAnimationFrame(()=>{
            this.loop()
        });
    }

    stopLoop(){
        if(this.stopAnimationFrameLine){
            window.cancelAnimationFrame(this.stopAnimationFrameLine);
        }
    }
}

